<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="template">

	<head th:replace="fragments/tabhead::head"></head>

	<body>
		<!-- BOM-->
		<th:block layout:fragment="content">
			<div style="margin-top:20px">
				<form class="layui-form search" id="searchForm">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label" style="width:100px">所属公司</label>
							<div class="layui-input-inline">
								<select name="companyId">
									<option value="1">深圳企智达有限公司</option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label" style="width:140px">父件编号/名称 </label>
							<div class="layui-input-inline" style="width:170px">
								<input type="tel" name="codeOrName" class="layui-input" placeholder="请输入父件名称或编码..." />
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label" style="width:100px">版本号</label>
							<div class="layui-input-inline" style="width:140px;">
								<input type="tel" name="version" class="layui-input" placeholder="请输入BOM版本..." />
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label" style="width:100px">审核状态</label>
							<div class="layui-input-inline" style="width:100px;">
								<select name="reviewStatus">
									<option value="" text="请选择审核状态" selected></option>
									<option value="false" selected>未审核</option>
									<option value="true" selected>已审核</option>
								</select>
							</div>
						</div>

						<div class="layui-inline">
							<div class="layui-input-inline">
								<button class="layui-btn" id="searchBtn" type="button">查询</button>
								<button class="layui-btn" id="addBtn" type="button" authcode='900240'>新增</button>
							</div>
						</div>

					</div>
				</form>
			</div>
			<table class="layui-table" id="tab" lay-filter="tab"></table>
			<script>
				var ins, page, trobj, editdialogindex;
				$(function() {
					layui.use(['table', 'layer'], function() {
						var table = layui.table;
						ins = table.render({ //其它参数在此省略
							elem: "#tab",
							//初始化加载网格
							url: '/eng/bom/data',
							cols: [
								[
									{ field: 'companyName', width: 180, title: '所属公司' },
									{ field: 'coding', width: 100, sort: true, title: '父件编码' },
									{ field: 'name', width: 100, title: '父件名称' },
									{ field: 'spec', width: 135, sort: true, title: '规格型号' },
									{ field: 'version', width: 80, sort: true, title: '版本号' },
									{ field: 'measurementUName', width: 180, sort: true, title: '计量单位' },
									{ field: 'rateFinish', width: 80, title: '成品率' },
									{ field: 'level', width: 177, title: 'BOM级次' },
									{ field: 'isDefault', width: 80, sort: true, title: '是否默认BOM', templet: '#strueorfalse' },
									{ field: 'isNoUsed', width: 80, sort: true, title: '是否停用', templet: '#trueorfalse' },
									{ field: 'modifyName', width: 135, sort: true, title: '修改人' },
									{ field: 'modifyTime', width: 135, sort: true, title: '修改日期' },
									{ field: 'reviewStatus', width: 135, sort: true, title: '审核状态', templet: '#status' },
									{ field: 'reviewName', width: 135, sort: true, title: '审核人' },
									{ field: 'reviewTime', width: 135, sort: true, title: '审核日期' },
									{ field: 'creatorName', width: 135, sort: true, title: '创建人' },
									{ field: 'ctime', width: 135, sort: true, title: '创建时间' },
									{ field: 'remark', width: 135, sort: true, title: '备注' },
									{ align: 'center', fixed: 'right', width: 300, toolbar: '#toolbar', title: '操作' }
								]
							],
							page: true //是否显示分页
								,
							limits: [10, 20, 50],
							limit: 10 //每页默认显示的数量
								//method: 'post'
								,
							done: function(res, curr, count) {
								page = curr;
							}
						});
						table.on("tool(tab)", function(obj) {
							var data = obj.data; //获得当前行数据
							var layEvent = obj.event; //获得 lay-event 对应的值
							trobj = obj;
							var tr = obj.tr; //获得当前行 tr 的DOM对象
							var id = obj.data.id
							var bomName = obj.data.name
							var bomVersion = obj.data.version
							if(layEvent == 'edit') {
								$.post('/eng/bom/goforedit?companyid=1&id=' + id, {}, function(str) {
									editdialogindex = layer.open({
										title: "修改BOM父件信息",
										type: 1,
										zIndex: 10,
										maxmin: true,
										area: ['1300px', '600px'],
										content: str //注意，如果str是object，那么需要字符拼接。
									});
								});
							} else if(layEvent == 'del') {
								layer.confirm('确定删除【名称：' + bomName + ' 版本号：' + bomVersion + '】？', { icon: 3, title: '提示' }, function(index) {
									//向服务端发送删除指令
									$.post("/eng/bom/deleteBom?companyid=1&id=" + id, {}, function(result) {
										if(result.code == 200) {
											obj.del(); //删除对应行（tr）的DOM结构
											layer.close(index);
											layer.msg(result.message, { icon: 6 });
											ins.reload({ url: '/eng/bom/data' });
											return false;
										} else {
											layer.msg(result.message, { icon: 0 });
											layer.close(index);
											return false;
										}
									});
								});
								return false;
							} else if(layEvent == "review") {
								layer.confirm('确定审核【名称：' + bomName + ' 版本号：' + bomVersion + '】？', { icon: 3, title: '提示' }, function(index) {
									if(data.reviewStatus == true) {
										layer.msg("物料已审核，不能审核！！", { icon: 4 });
										layer.close(index);
									} else {
										$.post('/eng/bom/audt?id=' + data.id, {}, function(result) {
											if(result.code == 200) {
												layer.msg(result.message, { icon: 6 });
												ins.reload({
													url: '/eng/bom/data'
												});
												return false;
											} else {
												layer.msg(result.message, { icon: 2 });
												return false;
											}
										});
										layer.close(index);
									}
								});
							} else if(layEvent == "celview") {
								layer.confirm('确定弃审【名称：' + bomName + ' 版本号：' + bomVersion + '】？', { icon: 3, title: '提示' }, function(index) {
									if(data.reviewStatus == 0) {
										layer.msg("单据未审核，不能弃审！！", { icon: 4 });
										layer.close(index);
									} else {
										//向服务端发送删除指令
										$.post('/eng/bom/unaudt?id=' + data.id, {}, function(result) {
											if(result.code == 200) {
												obj.update({
													reviewStatus: false
												});
												layer.msg(result.message, { icon: 6 });
												ins.reload({
													url: '/eng/bom/data'
												});
												return false;
											} else {
												layer.msg(result.message, { icon: 2 });
												return false;
											}
										});
										layer.close(index);
									}
								});
							}else if(layEvent == "childedit") {
								menuOpen("/eng/bom/childedit?bomId="+data.id,'父件BOM【名称：' + bomName + ' 版本号：' + bomVersion + '】明细');
								//子件编辑跳转页面
								///location.href="/eng/bom/childedit?bomId="+data.id;
							}
						});

						//查询按钮事件
						$("#searchBtn").click(function() {
							var table = layui.table;
							ins.reload({ url: '/eng/bom/search', where: $("#searchForm").serializeJson() })
						});

						//新增按钮事件
						$("#addBtn").click(function() {
							var table = layui.table;
							$.post('/eng/bom/goforadd?companyid=1', {}, function(str) {
								//打开  这个异步加载的html页面
								editdialogindex = layer.open({
									title: "新增BOM父件信息",
									type: 1,
									zIndex: 10,
									maxmin: true,
									area: ['1300px', '600px'],
									content: str //注意，如果str是object，那么需要字符拼接。
								});
							});
						});
					})
				});
			</script>
			<script type="text/html" id="trueorfalse">
				{{#if (d.isNoUsed==false){}}
				<span style="color:red;">否</span> {{# }else if(d.isNoUsed==true){}}
				<span style="color:green;">是</span> {{# }}}
			</script>

			<script type="text/html" id="strueorfalse">
				{{#if (d.isDefault==false){}}
				<span style="color:red;">否</span> {{# }else if(d.isDefault==true){}}
				<span style="color:green;">是</span> {{# }}}
			</script>

			<script type="text/html" id="status">
				{{#if (d.reviewStatus==false){}}
				<span style="color:red;">未审核</span> {{# }else if(d.reviewStatus==true){}}
				<span style="color:green;">已审核</span> {{# }}}
			</script>
			<script type="text/html" id="toolbar">
				<a class="layui-btn layui-btn-mini" lay-event="childedit" authcode="900245">明细</a>
				{{#if (d.reviewStatus==false){}}
				<a class="layui-btn layui-btn-mini" lay-event="edit" authcode="900241">父件修改</a>
				<a class="layui-btn layui-btn-mini" lay-event="review" authcode="900242">审核</a>
				<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del" authcode="900244">删除</a>
				{{# }else if(d.reviewStatus==true){}}
				<a class="layui-btn layui-btn-mini" lay-event="celview" authcode="900243">弃审</a>
				{{# }}}
			</script>
			<!--<div th:replace="/department/model::modelDiv"></div>-->
		</th:block>
	</body>

</html>